package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {


    @Autowired
    NursingProjectMapper nursingProjectMapper;
    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    @Cacheable(value = "zzyl:project", key = "#root.methodName + ':' + #pageQueryDto.hashCode()")
    public PageBean<NursingProjectVo> findByPage(NursingProjectPageQueryDto pageQueryDto) {
        PageHelper.startPage(pageQueryDto.getPageNum(), pageQueryDto.getPageSize());
        //PageBean of:将Page对象的属性取出并返回一个指定类型的PageBean对象
        return PageBean.of(nursingProjectMapper.findByPage(pageQueryDto), NursingProjectVo.class);
    }

    @Override
    public void add(NursingProjectDto dto) {
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProject.setCreateTime(LocalDateTime.now());//添加和更新在Mapper层传入pojo对象
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setRemark(nursingProject.getName());
        nursingProject.setCreateBy(dto.getId());
        nursingProject.setUpdateBy(dto.getId());
        nursingProjectMapper.add(nursingProject);
    }

    @Override
    public NursingProjectVo read(Long id) {

        return nursingProjectMapper.read(id);
    }

    @Override
    @CacheEvict(value = "zzyl:project", allEntries = true)
    public void update(NursingProjectDto dto) {
        NursingProject project = BeanUtil.toBean(dto, NursingProject.class);
        project.setUpdateTime(LocalDateTime.now());
        project.setUpdateBy(dto.getId());
        nursingProjectMapper.update(project);

    }

    @Override
    public void updateStatus(Long id, Integer status) {
        //mapper中的方法中只能为增删改查
        //通过builder构建对象
        NursingProject nursingProject = NursingProject.builder().id(id).status(status).build();
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    public void delete(Long id) {
        //判断护理项目是否启用
        if (nursingProjectMapper.read(id).getStatus() == 1) {
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        //2. 判断该护理项目是否关联护理计划，如果关联，抛异常
        //select * from nursing_project_plan where project_id = id
        List<NursingProjectPlan> list = nursingProjectPlanMapper.getByProjectId(id);
        if (CollUtil.isNotEmpty(list)) {
            //说明有关联数据
            throw new BaseException(BasicEnum.DELETE_PROJECT_FAIL);
        }
        //3. 删除护理项目
        nursingProjectMapper.delete(id);
    }

    @Override
    @Cacheable(value = "zzyl:project", key = "#root.methodName")
    public List<NursingProject> findAll() {

        return nursingProjectMapper.findAll();
    }

}
